package com.hnl.action.talent;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.aware.HNLTalentAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.TalentDAO;
import com.hnl.to.Talent;
import com.hnl.to.wrapper.TalentWrapper;
import com.hnl.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadTalentInfo extends HttpServlet implements HNLTalentAware {
	public static final Logger LOG = LoggerFactory
			.getLogger(LoadTalentInfo.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {
			Talent talent = new Talent();
			String talentID = req.getParameter(ATTRIBUTE_TALENT_ID);
			if (talentID != null && talentID.length() != 0) {
				TalentDAO talentDAO = HNLDAOFactory.getDAOFactory(
						HNLDAOFactory.HNLDAOFACTORY).getTalentDAO();
				talent = talentDAO.loadTalent(Integer.parseInt(talentID));
				TalentWrapper talentWrapper = new TalentWrapper(talent);
				talentWrapper.getBirthdayFormat();
				writer.println(JSONResponseUtil
						.getJSONObjectString(talentWrapper));
			} else {
				writer.println(JSONResponseUtil.getJSONObjectString(talent));
			}
		} catch (DAOException e) {
			LOG.error("error laoding Talent", e);
		} finally {
			writer.close();
		}
	}
}
